home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 2 / Atari Mega Archive CD - Volume 2.iso / minix / up1510b.tgz / up1510b / src / commands / width.c < prev    next >
C/C++ Source or Header  |  1990-07-23  |  3KB  |  118 lines

  1. /* width - force a file to a fixed width    Author: Andy Tanenbaum */
  2.  
  3. /* This program reads an input file (by default, stdin), and forces each
  4.  * line to a certain length (by default 80 columns).  Lines that are
  5.  * shorter than this are padded with spaces.  Lines that are longer are
  6.  * truncated.  Tabs are expanded to spaces in the output.
  7.  *
  8.  * Examples:    width x y     # copy x to y and make all lines 80 cols
  9.  *        width x        # copy x to stdout and force all lines to 80
  10.  *        width -40 x    # copy x to stdout and force to 40 cols
  11.  *        width -30 x y    # copy x to y and force to 30 cols
  12.  *        width -20    # copy stdin to stdout and force to 20 cols
  13.  */
  14.  
  15. #include <stdio.h>
  16.  
  17. #define MAX_WIDTH    1024    /* longest acceptable line */
  18. #define TAB_WIDTH          8    /* spacing between tab stops */
  19.  
  20. main(argc, argv)
  21. int argc;
  22. char *argv[];
  23. {
  24.  
  25.   int width, k;
  26.   char *p;
  27.   FILE *in, *out;
  28.  
  29.   /* Process command line. */
  30.   p = argv[1];
  31.   k = 1;
  32.   width = 80;
  33.   if (argc > 1 && *p == '-') {
  34.     width = atoi(p + 1);
  35.     if (width < 1 || width > MAX_WIDTH) usage(argv[0]);
  36.     k = 2;
  37.   }
  38.   if (argc == k) {
  39.     /* No file arguments. */
  40.     process(stdin, stdout, width);
  41.   } else if (argc == k + 1) {
  42.     /* For example: width file   or   width -30 file */
  43.     if ((in = fopen(argv[k], "r")) == (FILE *) NULL)
  44.         cant_open(argv[0], argv[k]);
  45.     process(in, stdout, width);
  46.   } else if (argc == k + 2) {
  47.     /* For example, width inf outf  or   width -30 inf outf */
  48.     if ((in = fopen(argv[k], "r")) == (FILE *) NULL)
  49.         cant_open(argv[0], argv[k]);
  50.     if ((out = fopen(argv[k + 1], "w")) == (FILE *) NULL)
  51.         cant_open(argv[0], argv[k]);
  52.     process(in, out, width);
  53.   } else {
  54.     usage(argv[0]);
  55.   }
  56.   exit(0);
  57. }
  58.  
  59.  
  60. process(in, out, width)
  61. FILE *in, *out;
  62. int width;
  63. {
  64. /* Copy in to out and force all the lines to width. */
  65.  
  66.   int col, spaces;
  67.   register char *p, *q;
  68.   char in_buf[MAX_WIDTH + 1], out_buf[MAX_WIDTH + 1];
  69.  
  70.   while (fgets(in_buf, MAX_WIDTH, in) != (char *) NULL) {
  71.     /* This loop executed once for each line in the input file. */
  72.     p = in_buf;
  73.     q = out_buf;
  74.     col = 0;
  75.     while (*p != 0) {
  76.         /* This loop executed once for each character in the line. */
  77.         if (*p != '\t') {
  78.             /* Not a tab. */
  79.             *q++ = *p++;
  80.             col++;
  81.         } else {
  82.             /* Tab. */
  83.             spaces = TAB_WIDTH - (col % TAB_WIDTH);
  84.             col += spaces;
  85.             while (spaces--) *q++ = ' ';
  86.             p++;
  87.         }
  88.     }
  89.     if (*(q - 1) == '\n') *(q - 1) = ' ';
  90.     while (q < &out_buf[width]) *q++ = ' ';
  91.     out_buf[width] = '\n';
  92.     out_buf[width + 1] = 0;
  93.     fputs(out_buf, out);
  94.   }
  95. }
  96.  
  97.  
  98. cant_open(program, file)
  99. char *program;
  100. char *file;
  101. {
  102.   fputs(program, stderr);
  103.   fputs(": cannot open ", stderr);
  104.   fputs(file, stderr);
  105.   putc('\n', stderr);
  106.   exit(1);
  107. }
  108.  
  109.  
  110. usage(s)
  111. char *s;
  112. {
  113.   fputs("Usage: ", stderr);
  114.   fputs(s, stderr);
  115.   fputs(" [-<width>] [infile [outfile] ]\n", stderr);
  116.   exit(1);
  117. }
  118.